package com.ssyx.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyx.common.exception.SsyxException;
import com.ssyx.common.result.ResultCodeEnum;
import com.ssyx.enums.PaymentStatus;
import com.ssyx.enums.PaymentType;
import com.ssyx.model.order.OrderInfo;
import com.ssyx.model.order.PaymentInfo;
import com.ssyx.payment.feign.OrderFeignClient;
import com.ssyx.payment.feign.ProductFeignClient;
import com.ssyx.payment.mapper.PaymentInfoMapper;
import com.ssyx.payment.service.PaymentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * ClassName:PaymentInfoServiceImpl
 * Description:
 *
 * @Author kay
 * @Create 2024/5/24 17:04
 * @Version 1.0
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo>
                implements PaymentInfoService {
    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public PaymentInfo getPaymentInfo(String orderNo, PaymentType paymentType) {
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo,orderNo).eq(PaymentInfo::getPaymentType,paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PaymentInfo savePaymentInfo(String orderNo, PaymentType paymentType) {
        //根据orderNo,查询订单相关信息,远程调用
        OrderInfo order = orderFeignClient.getOrderInfoByOrderNo(orderNo);
        if(null == order) {
            throw new SsyxException(ResultCodeEnum.DATA_ERROR);
        }
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentType(paymentType);

        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(order.getId());
        paymentInfo.setUserId(order.getUserId());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
        String subject = "userID:"+order.getUserId()+"下订单";
        paymentInfo.setSubject(subject);
        //paymentInfo.setTotalAmount(order.getTotalAmount());
        //为了测试
        paymentInfo.setTotalAmount(new BigDecimal("0.01"));

        baseMapper.insert(paymentInfo);
        return paymentInfo;
    }

    //// 成功后,修改支付记录表状态,已支付;修改订单记录已支付,并扣减库存
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void paySuccess(String orderNo, Map<String, String> resultMap) {

        //查询当前支付状态,如果没有支付,则返回
        LambdaQueryWrapper<PaymentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        if (paymentInfo.getPaymentStatus() != PaymentStatus.UNPAID) {
            return;
        }
        //如果支付,修改支付记录表状态,更新
        paymentInfo.setPaymentStatus(PaymentStatus.PAID);
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackContent(resultMap.toString());
        paymentInfo.setCallbackTime(new Date());
        baseMapper.updateById(paymentInfo);
        //修改订单信息表状态,已支付后的状态
        orderFeignClient.orderPay(orderNo);

        //扣减产品表库存
        productFeignClient.updateStockByOrderNo(orderNo);
    }
}
